package com.cortex.carambola.database;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.cortex.carambola.summary.CarambolaSummary;
import com.cortex.carambola.utilities.Utilities;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataBase {

	private SQLiteDatabase db;
	private DbHelper dbHelper;
	private final static  String TABLE_NAME = DbHelper.TABLE_NAME;
	private final static String[] ALL_COLUMNS = {DbHelper.COL_ID,DbHelper.COL_NAME,DbHelper.COL_SUMMARY,
		DbHelper.COL_SENTENCES,DbHelper.COL_KEYWORDS,DbHelper.COL_IS_QUOTES,
		DbHelper.COL_CHARACTERS,DbHelper.COL_URL};

	private final static String COL_ID = DbHelper.COL_ID;
	private final static String COL_NAME = DbHelper.COL_NAME;
	private final static String COL_SUMMARY = DbHelper.COL_SUMMARY;
	private final static String COL_SENTENCES = DbHelper.COL_SENTENCES;
	private final static String COL_KEYWORDS = DbHelper.COL_KEYWORDS;
	private final static String COL_IS_QUOTES = DbHelper.COL_IS_QUOTES;
	private final static String COL_CHARACTERS = DbHelper.COL_CHARACTERS;
	private final static String COL_URL = DbHelper.COL_URL;

	public DataBase(Context context){
		dbHelper = new DbHelper(context);
	}
	private void open(){
		db = dbHelper.getWritableDatabase();
	}
	private void close(){
		dbHelper.close();
	}

	public boolean insert(CarambolaSummary summary){
		boolean retValue = true;
		this.open();
		try
		{
			ContentValues cv = new ContentValues();
			cv.put(COL_NAME, summary.getName());
			cv.put(COL_SUMMARY, summary.getSummary());
			cv.put(COL_SENTENCES, summary.getSentences());
			cv.put(COL_KEYWORDS, Utilities.list2csv(summary.getKeywords()));
			cv.put(COL_IS_QUOTES, Utilities.boolean2integer(summary.isQuotes()));
			cv.put(COL_CHARACTERS, summary.getCharacters());
			cv.put(COL_URL, summary.getUrl());
			
			summary.setId(db.insert(TABLE_NAME, null, cv));
		}
		catch(Exception e)
		{
			Log.d("gal",e.getMessage());
			retValue = false;
		}
		finally
		{
			this.close();
		}
		return retValue;
	}

	public boolean delete(CarambolaSummary summary)
	{
		boolean retValue = true;
		this.open();
		try{
			db.delete(TABLE_NAME, COL_ID + "='" + summary.getId() + "'", null);
		}
		catch(Exception e){
			Log.d("gal",e.getMessage());
			retValue = false;
		}
		finally
		{
			this.close();
		}
		return retValue;
	}

	public List<CarambolaSummary> readAllData()
	{
		Cursor c;
		List<CarambolaSummary> list;
		open();
		try
		{
			c = db.query(TABLE_NAME, ALL_COLUMNS, null, null, null, null, null,null);
			list = buildSummaryList(c);
		}
		finally
		{
			close();
		}
		return list;
	}   

	private List<CarambolaSummary> buildSummaryList(Cursor c)
	{
		List<CarambolaSummary> summaryList = new ArrayList<CarambolaSummary>();

		int id;
		String name,summary,sentences,characters,url;
		List<String> keywords;
		boolean isQoutes;
		c.moveToFirst();
		if(c.isFirst()){
			do
			{
				id = c.getInt(0);
				name = c.getString(1);
				summary = c.getString(2);
				sentences = c.getString(3);
				keywords = Utilities.csv2ArrayList(c.getString(4));
				isQoutes = Utilities.integer2boolean(c.getInt(5));
				characters = c.getString(6);
				url = c.getString(7);
				summaryList.add(new CarambolaSummary(id,name, summary, 
						sentences, characters, keywords, isQoutes,url));
			}while(c.moveToNext());
		}
		c.close();
		return summaryList;
	}
	public void deleteAll() {
		this.open();
		try{
			db.delete(TABLE_NAME,null,null);
		}finally{
			close();
		}
	}
}
